// Copyright 2013 Ronald Schlenker, www.technewlogic.de.
// 
// This file is part of Byond.
// 
// Byond is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
// 
// Byond is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with Byond.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using Microsoft.Win32;
using Byond.UIRuntime;
using Utils;
using Utils.Documentation;

namespace Byond.PatchUiControls
{
	[Doc]
	public class SamplePicker : UiControl
	{
		static SamplePicker()
		{
			DefaultStyleKeyProperty.OverrideMetadata(
				typeof(SamplePicker),
				new FrameworkPropertyMetadata(typeof(SamplePicker)));
		}

		public SamplePicker()
		{
			EnableBindToMe = false;
			_dialog = new OpenFileDialog();
			ShowDialogCommand = new RelayCommand(ShowSamplePickDialog);
		}

		private readonly OpenFileDialog _dialog;

		#region Popup

		private Popup _popup;

		public ICommand ShowDialogCommand { get; private set; }

		private readonly ObservableCollection<SampleViewModel> _samples = new ObservableCollection<SampleViewModel>();
		public IEnumerable<SampleViewModel> Samples { get { return _samples; } }

		#endregion

		private SampleViewModel _selectedSample;
		public SampleViewModel SelectedSample
		{
			get { return _selectedSample; }
			set
			{
				_selectedSample = value;
				if (_popup != null)
					_popup.IsOpen = false;
				OnPropertyChanged("SelectedSample");
				OnPropertyChanged("FileName");
			}
		}

		[Doc]
		public string FileName
		{
			get { return SelectedSample != null ? SelectedSample.FileName : ""; }
			set { SelectedSample = new SampleViewModel(value); }
		}

		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();

			_popup = Template.FindName("PART_Popup", this) as Popup;
			var border = Template.FindName("PART_Border", this) as Border;
			border.PreviewMouseLeftButtonUp += (s, e) => PrepareSamplePick(_popup);
		}

		private void PrepareSamplePick(Popup popup)
		{
			if (string.IsNullOrEmpty(FileName))
				ShowSamplePickDialog();
			else
			{
				try
				{
					_samples.Clear();
					Directory
						.GetFiles(Path.GetDirectoryName(FileName))
						.Select(it => new SampleViewModel(it))
						.ForEach(_samples.Add);
					popup.IsOpen = true;
				}
				catch (Exception e)
				{
					// TODO: Logging
					ShowSamplePickDialog();
				}
			}
		}

		private void ShowSamplePickDialog()
		{
			if (!_dialog.ShowDialog().Value)
				return;
			FileName = _dialog.FileName;
			WasChangedByUser = true;
		}
	}

	public class SampleViewModel
	{
		public SampleViewModel(string fileName)
		{
			FileName = fileName;
		}

		public string FileName { get; private set; }

		public string DisplayFileName
		{
			get { return string.IsNullOrEmpty(FileName) ? string.Empty : Path.GetFileName(FileName); }
		}
	}
}